/**
 *  This file is part of TaoBaoItemImporter.
 *
 *   TaoBaoItemImporter is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   TaoBaoItemImporter is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with TaoBaoItemImporter.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.googlecode.ulysses.taobao;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * A simple class for Mongodb communication.
 * 
 * @author Ulysses <robman1070@gmail.com>
 *
 */
public class Connector {
	private String host= "localhost";
	private int port= 27017;
	private String user= null;
	private char[] security = null;
	private String db = null;
	private String collection= null;
	
	private Mongo mongo = null;
	private DB dbObj = null;
	private DBCollection collObj = null;
	
	public Connector() {
	}
	
	public Connector(String host) {
		this.host = host;
	}
	
	public Connector(String host, int port) {
		this.host = host;
		this.port = port;
	}
	
	public Connector(String host, int port, String user, char[] security) {
		this.host = host;
		this.port = port;
		this.user = user;
		this.security = security;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public char[] getSecurity() {
		return security;
	}

	public void setSecurity(char[] security) {
		this.security = security;
	}

	public String getDb() {
		return db;
	}

	public void setDb(String db) {
		this.db = db;
	}

	public String getCollection() {
		return collection;
	}

	public void setCollection(String collection) {
		this.collection = collection;
	}
	
	public Mongo getMongo() {
		return mongo;
	}

	public void connect() throws UnknownHostException, MongoException {
		mongo = new Mongo(host, port);
	}
	
	/**
	 * Connect to a database.
	 * 
	 * @return DB object
	 */
	public DB getDBObj() {
		return getDBObj(this.db);
	}
	
	/**
	 * Connect to a database specified.
	 * 
	 * @param database Name of the database to connect
	 * @return DB object
	 */
	public DB getDBObj(String database) {
		this.db = database;
		dbObj = mongo.getDB(database);
		
		return dbObj;
	}
	
	/**
	 * @return DBCollection A collection handler
	 */
	public DBCollection getCollectionObj() {
		return getCollectionObj(db, collection);
	}
	
	/**
	 * @param collection Name of the collection
	 * @return DBCollection A collection handler
	 */
	public DBCollection getCollectionObj(String collection) {
		this.collection = collection;
		return getCollectionObj(db, collection);
	}
	
	/**
	 * Connect to a collection and return the handler.
	 * 
	 * @param database Name of the database
	 * @param collection Name of the collection
	 * @return DBCollection A collection handler
	 */
	public DBCollection getCollectionObj(String database, String collection) {
		getDBObj(database);
		
		this.collection = collection;
		collObj = dbObj.getCollection(collection);
		
		return collObj;
	}
	

}
